package main

import (
	"fmt"
	"math/rand"
	"time"
)

type job struct {
	x int64
}

type res struct {
	job *job
	res int
	id  int
}

// 随机生成int64
func producer(jobCh chan<- *job) {
	for {
		rand.Seed(time.Now().UnixNano())
		x := rand.Int63()
		jobCh <- &job{
			x: x,
		}
		time.Sleep(500 * time.Millisecond)
	}
}

// 对int64求和
func intSum(id int, jobCh <-chan *job, resCh chan<- *res) {
	for item := range jobCh {
		n := item.x
		var s = 0
		for n > 0 {
			s = s + int(n%10)
			n = n / 10
		}
		resCh <- &res{
			job: item,
			res: s,
			id:  id,
		}
	}
}

func main() {
	var jobCh = make(chan *job, 100)
	var resCh = make(chan *res, 100)
	go producer(jobCh)
	for i := 0; i < 24; i++ {
		go intSum(i, jobCh, resCh)
	}
	for res := range resCh {
		fmt.Printf("[%d] %d >>> %d \n", res.id, res.job.x, res.res)
	}
}
